/***********************************************************************************************************************
* Copyright (C) . All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : main.c
* Version      :  
* Device(s)    : BAT32G137
* Tool-Chain   : MDK(armcc)
* Description  : This file is a template.
* Creation Date: 2019/4/30
***********************************************************************************************************************/

/***********************************************************************************************************************
Macro Definitions
***********************************************************************************************************************/

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include <stdio.h>
#include "BAT32G137.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
volatile uint32_t g_ticks;

void delayMS(uint32_t n)
{
		g_ticks = n;
		while(g_ticks);
}

int main()
{
	uint32_t msCnt; 	// count value of 1ms
	uint32_t err;

//-----------------------------------------------------------------------
// Systick setting 
//-----------------------------------------------------------------------   
	SystemCoreClockUpdate();
	msCnt = SystemCoreClock / 1000;
	SysTick_Config(msCnt); 
	delayMS(500);
	
//-----------------------------------------------------------------------
// P40/SWDIO and P137/SWCLK Toggle
//----------------------------------------------------------------------- 
	/* disable SWD interface */
	DBG->DBGSTOPCR |= DBG_DBGSTOPCR_SWDIS_Msk;
	
	PORT->P4    = 0x01U;  
	PORT->PM4  &= ~(1<<0); // P40
	PORT->P13    = 0x80U;  
	PORT->PM13  &= ~(1<<7); // P137
	while(1)
	{
		PORT->P4 ^= (1<<0); 	// Toggle P40
		PORT->P13 ^= (1<<7); 	// Toggle P137
	}
}

/***********************************************************************************************************************
* Function Name: SysTick Handler
* Description  : Decreament the g_ticks value
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void SysTick_Handler(void)
{
	g_ticks--;
}

